package com.thingsgrid.appmonitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thingsgrid.appmonitor.entity.Application;
import com.thingsgrid.appmonitor.entity.ApplicationMonitor;
import com.thingsgrid.appmonitor.mapper.ApplicationMapper;
import com.thingsgrid.appmonitor.service.ApplicationService;
import lombok.AllArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 应用状态监控实现类
 */
@Service
@AllArgsConstructor
public class ApplicationServiceImpl implements ApplicationService {
	private ApplicationMapper applicationMapper;
	private MongoTemplate mongoTemplate;

	/**
	 * 应用信息分页
	 *
	 * @param page
	 * @param deviceId
	 * @return
	 */
	@Override
	public Page<Application> applicationByPage(Page<Application> page, String deviceId) {
		Page<Application> applicationPage = applicationMapper.applicationByPage(page, deviceId);//从mapper得到数据
		List<Application> appList = applicationPage.getRecords();//从Page中取出数据存到list集合中
		//遍历list集合,给属性赋值
		for (Application application : appList) {
			String application_id = application.getApplicationId();//从list集合中取到id
			int state=applicationMapper.getApplicationState(application_id,deviceId);
			application.setApplicationState(state);
			Query query = new Query();
			query.addCriteria(Criteria.where("applicationId").is(application_id));
			List<ApplicationMonitor> appMonitor = mongoTemplate.find(query, ApplicationMonitor.class, "application_monitor");//通过id应用的监控数据
			//对appMonitor进行排序
			Collections.sort(appMonitor, new Comparator<ApplicationMonitor>() {
				@Override
				public int compare(ApplicationMonitor o1, ApplicationMonitor o2) {
					//升序排序
					if (o1.getCreateTime() > o2.getCreateTime()) {
						return -1;
					}
					if (o1.getCreateTime() == o2.getCreateTime()) {
						return 0;
					}
					return 1;
				}
			});
			for (int i = 0; i < appMonitor.size(); i++) {
				//取出升序排序的第一条数据===就是最新的一条数据
				String cpu = appMonitor.get(0).getApplicationCpu();
				String memory = appMonitor.get(0).getApplicationMemory();
				String disk = appMonitor.get(0).getApplicationDisk();
				String flow = appMonitor.get(0).getApplicationFlow();
				//赋值
				application.setApplicationCpu(cpu);
				application.setApplicationMemory(memory);
				application.setApplicationDisk(disk);
				application.setApplicationFlow(flow);
			}
		}
		return applicationPage.setRecords(appList);//返回page里面数据的遍历结果
	}

	/**
	 * 根据应用id删除指定应用
	 */
	@Override
	public boolean deleteByApplicationId(String applicationId, String deviceId) {
		boolean status = applicationMapper.deleteByApplicationId(applicationId, deviceId);
		return status;
	}

	/**
	 * 根据应用id禁用
	 */
	@Override
	public boolean forbiddenByApplicationId(String applicationId, String deviceId) {
		boolean status = applicationMapper.forbiddenByApplicationId(applicationId, deviceId);
		return status;
	}

	/**
	 * 根据应用id启用
	 */
	@Override
	public boolean startByApplicationId(String applicationId, String deviceId) {
		boolean status = applicationMapper.startByApplicationId(applicationId, deviceId);
		return status;
	}


}
